package com.woniuxy.service.impl;


import com.woniuxy.dao.AccountMapper;
import com.woniuxy.model.Account;
import com.woniuxy.service.AccountService;
import com.woniuxy.util.SqlSessionUtil;

/**
 * ClassName:AccountServiceImpl
 * Package:impl
 *
 * @date:2021/11/2 4:04 下午
 * @author: Tiangle
 */
public class AccountServiceImpl implements AccountService {
    @Override
    public boolean transfer(String name1, String name2, Double money) {
        AccountMapper mapper = SqlSessionUtil.getMapper(AccountMapper.class);
        Account account1 = mapper.getAccountByName(name1);
        Account account2 = mapper.getAccountByName(name2);

        if (account1==null){
            return false;
        }
        if (account2==null){
            return false;
        }

        if (account1.getBalance()<money){
            return false;
        }

        account1.setBalance(account1.getBalance()-money);
        account2.setBalance(account2.getBalance()+money);

        mapper.updateAccount(account1);
        mapper.updateAccount(account2);
        return true;
    }

    public String selectA(String name){
        AccountMapper mapper = SqlSessionUtil.getMapper(AccountMapper.class);
        Account accountByName = mapper.getAccountByName(name);
        return accountByName.toString();
    }

}
